package com.gw.smscheck.service.sys.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gw.smscheck.dao.sys.DepartmentMapper;
import com.gw.smscheck.entity.sys.Department;
import com.gw.smscheck.entity.sys.DeptTreeGridModel;
import com.gw.smscheck.entity.sys.QueryResult;
import com.gw.smscheck.entity.sys.TreeModel;
import com.gw.smscheck.service.sys.DepartmentService;
import com.gw.smscheck.util.ResultObj;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* 部门管理业务层
* @ClassName: DepartmentService 
* @author zhouxiaoying
* @date 2017年10月9日 上午8:54:23 
 */
@Service
public class DepartmentServiceImpl implements DepartmentService
{

    @Autowired
    private DepartmentMapper departmentMapper;
    
    private Logger logger = Logger.getLogger(DepartmentServiceImpl.class);
    /**
     * 通过code获取
     * @param dataDictionary
     * @return
     */

    public String getByDescribes(Department department)
    {
        List<DeptTreeGridModel> list = departmentMapper.getByDescribes(department);
        List<DeptTreeGridModel> newList = new ArrayList<DeptTreeGridModel>();
        for (DeptTreeGridModel Children : list)
        {
            getChildren(Children);
            newList.add(Children);
        }
        return ResultObj.successResultObj(list);
    }

    /**
     * 分页查询
     * @param department
     * @return
     */
    @Override
    public List<Department> pageSelect(Department department) {
        PageHelper.startPage(department.getPage(), department.getRows(), "sort ASC");// 排序
        List<Department> resultList = departmentMapper.pageSelect(department);
//        PageInfo<Department> pageInfo = new PageInfo<>(resultList);
        return resultList;
    }

    /**
     * 根据id查询
     * @param department
     * @return
     */
    @Override
    public Department findById(Department department) {
        return departmentMapper.findById(department);
    }

    /**
     *
     * TODO描述 递归
     * @Title: getChildren
     * @param  @param node
     * @return void
     * @throws
     */
    public void getChildren(DeptTreeGridModel node)
    {
        List<DeptTreeGridModel> listChildren = departmentMapper.getChildrenById(node);
        if (listChildren.size() > 0)
        {
            node.setChildren(listChildren);
            for (DeptTreeGridModel Children : listChildren)
            {
                getChildren(Children);
            }
        }
    }
    /**
     * 部门树形表格信息
     * @param menu
     * @return
     * @throws Exception 
     */
    @Override
    public QueryResult<DeptTreeGridModel> findTreeDeptInfos(Department department) throws Exception
    {
        List<Department> parents = departmentMapper.queryFirstSerialDept(department);
        List<DeptTreeGridModel> result = new ArrayList<>();
        DeptTreeGridModel model = null;
        for (Department dept : parents)
        {
            model = new DeptTreeGridModel();
            BeanUtils.copyProperties(dept, model);
            // 计算孩子节点
            List<Department> childrens = departmentMapper.getChildrens(department.getId());
            if (!childrens.isEmpty())
            {
                model.setChildren(getTreeGrid(dept.getId()));
                logger.info(model.getDepartmentName() + "==========id");
            }
            result.add(model);
        }
        return new QueryResult<>(this.departmentMapper.getDepCount(department), result);
    }

    /**
     *递归回去子部门
     * @Title: getTreeGrid 
     * @param pId
     * @param @throws Exception    
     * @return List<MenuTreeGridModel>   
     * @throws
      */
    @Override
    public List<DeptTreeGridModel> getTreeGrid(Integer pId)
    {
        List<Department> menuInfos = departmentMapper.getChildrens(pId);
        List<DeptTreeGridModel> result = new ArrayList<>();
        DeptTreeGridModel model = null;
        for (Department department : menuInfos)
        {
            model = new DeptTreeGridModel();
            BeanUtils.copyProperties(department, model);
            // 计算孩子节点
            List<Department> childrens = departmentMapper.getChildrens(department.getId());
            if (!childrens.isEmpty())
            {
                model.setChildren(getTreeGrid(department.getId()));
                logger.info(model.getDepartmentName() + "==========");
            }
            result.add(model);
        }
        return result;
    }

    /**
     * 获取树结构下拉框
    * @Title: findAllMenus 
    * @param department
    * @param @throws Exception    
    * @return List<TreeModel>   
    * @throws
     */
    @Override
    public List<TreeModel> findAllMenus(Department department) throws Exception
    {
        // 获取一级菜单
        List<Department> menuInfos = departmentMapper.findDepartmentInfo(department);
        List<TreeModel> result = new ArrayList<>();
        TreeModel model = null;
        for (Department dept : menuInfos)
        {
            if (dept.getpId() == 0)
            {
                model = new TreeModel();
                model.setId(dept.getId());
                model.setText(dept.getDepartmentName());
                // 计算孩子节点：所有的非按钮
                Department parent = new Department();
                parent.setpId(dept.getId());
                List<Department> childrens = departmentMapper.findDepartmentInfo(parent);
                if (!childrens.isEmpty())
                {
                    model.setChildren(getChildrens(parent));
                }
                result.add(model);
            }
        }
        return result;
    }

    /**
    * 递归获取下级所有的子菜单
    * @Title: getChildrens 
    * @param parent
    * @param @throws Exception    
    * @return List<TreeModel>   
    * @throws
     */
    private List<TreeModel> getChildrens(Department parent) throws Exception
    {
        List<TreeModel> result = new ArrayList<>();
        TreeModel model = null;
        List<Department> menuInfos = departmentMapper.findDepartmentInfo(parent);
        for (Department dept : menuInfos)
        {
            model = new TreeModel();
            model.setId(dept.getId());
            model.setText(dept.getDepartmentName());
            // 计算孩子节点：下级菜单
            Department son = new Department();
            son.setpId(dept.getId());
            List<Department> childrens = departmentMapper.findDepartmentInfo(son);
            if (!childrens.isEmpty())
            {
                List<TreeModel> nbc = getChildrens(son);
                if (!nbc.isEmpty())
                {
                    model.setChildren(nbc);
                }
            }
            result.add(model);
        }
        return result;
    }

    /**
    * 添加部门
    * @Title: saveDepartment 
    * @param department
    * @return Integer   
    * @throws
     */
    @Override
    public Integer saveDepartment(Department department)
    {
        Integer i = departmentMapper.saveDepartment(department);

        // 自动生成index
        String str = String.format("%04d",department.getId());
        Integer tempId = department.getpId();
        if(tempId == 0){// 无父级
            department.setIndex(str);
        }else {
            // 查询父节点index
            Department pDepartment1 = new Department();
            pDepartment1.setId(department.getpId());
            String pStr = departmentMapper.findById(pDepartment1).getIndex();
            department.setIndex(pStr + "-" + str);
        }
        departmentMapper.setIndex(department);
        return i;

    }

    /**
    * 修改部门
    * @Title: updateDepartment 
    * @param department
    * @return Integer   
    * @throws
     */
    @Override
    public Integer updateDepartment(Department department)
    {
        return departmentMapper.updateDepartment(department);

    }

    /**
    * 根据id删除部门
    * @Title: updateDepartment 
    * @param id
    * @return Integer   
    * @throws
     */
    @Override
    public Integer deleteDepartment(Integer id)
    {
        return departmentMapper.deleteDepartment(id);

    }

    @Override
    public List<Department> queryDepartAll()
    {

        return departmentMapper.queryDepartAll();
    }

}
